from pandas import read_sql
from pandas import DataFrame
from db_tools import MysqlTool


class DatabaseOperations(MysqlTool):
    def read_in_type_data(self, intype_list=[]):
        intype_list_str = ",".join([str(i) for i in intype_list])
        sql = (
            f"SELECT * FROM pay WHERE in_type IN ({intype_list_str}) AND is_matched = 0 "
            f"AND ((`from` = 'IOS' AND version_code >= 156) "
            f"OR (`from` = '安卓' AND version_code >= 31))"
        )
        return read_sql(sql, self.engine)

    def update_to_mysql(self, selected_data, table="course_pay_info"):
        df_res = DataFrame([selected_data])
        self.upsert_df(df_res, table_name=table, primary_key="order_no")
        return self.execute_sql(
            f"UPDATE pay SET is_matched = 1 WHERE order_no = '{selected_data['order_no']}'"
        )

    def execute_sql_update(self, order_no, match_status):
        return self.execute_sql(
            f"UPDATE pay SET is_matched = {match_status} WHERE order_no = '{order_no}'"
        )
